/** ==================================================================================== **/
/** 							WIDGET ENTITY - DASHBOARD								 **/
/** ==================================================================================== **/

BootApplication.service('widgetService', [ 'log', '$http', 'configuration', '$q', "$translate", function( log, $http, appConstants, $q, translate ) {
	
    this.saveWidget = function(info, parametri){
    	try{
    		
			var requestData = new Object();
			if(angular.isDefined(info) && info != null){
				requestData.name = info.name;
				requestData.descrizione = info.descrizione;
				requestData.idUWidget = info.widget.id;
			}
			if(angular.isDefined(parametri) && parametri != null){
				requestData.filter = parametri;
			}
			requestData.sizeX = 1;
			requestData.sizeY = 1;
			requestData.show =false;
			requestData.row = 0;
			requestData.col = 0;
			
			var deferred = $q.defer();
			
			$http({
				method: 'POST',
				url: appConstants.getParam("Constants").URL_INSERT_WIDGET,
				data: requestData,
				responseType: "json",
				cache: false,
			})
			.success(function(data, status, headers, config) {
				deferred.resolve(data);
			})
			.error(function(data, status, headers, config) {
				/* */
				log.error("widgetService: saveWidget ---> " + ((angular.isDefined(data) && data != null ) ? angular.toJson(data) : data));
					
				deferred.reject(data);
			});
			
			return deferred.promise;
		}catch(e){
			/* */
			log.error("widgetService: saveWidget ---> " + e);
		}
    }
    
    this.deleteWidget = function(id){
    	try{

			var requestData = new Object();
			if(angular.isDefined(id) && id != null){
				requestData.idWidget = id;
			}
			var deferred = $q.defer();
			
			$http({
				method: 'GET',
				url: appConstants.getParam("Constants").URL_DELETE_WIDGET_BY_ID,
				params: requestData,
				responseType: "json",
				cache: false,
			})
			.success(function(data, status, headers, config) {
				deferred.resolve(data);
			})
			.error(function(data, status, headers, config) {
				/* */
				log.error("widgetService: deleteWidget ---> " + ((angular.isDefined(data) && data != null ) ? angular.toJson(data) : data));
					
				deferred.reject(data);
			});
			
			return deferred.promise;
    	}catch(e){
			/* */
			log.error("widgetService: deleteWidget ---> " + e);
		}
    }
    
    this.getDettaglioWidget = function(id){
    	try{
    		
			var requestData = new Object();
			if(angular.isDefined(id) && id != null){
				requestData.id = id;
			}
			var deferred = $q.defer();
			
			$http({
				method: 'GET',
				url: appConstants.getParam("Constants").URL_SEARCH_CUSTOM_WIDGET_BY_ID,
				params: requestData,
				responseType: "json",
				cache: false,
			})
			.success(function(data, status, headers, config) {
				deferred.resolve(data);
			})
			.error(function(data, status, headers, config) {
				/* */
				log.error("widgetService: getDettaglioWidget ---> " + ((angular.isDefined(data) && data != null ) ? angular.toJson(data) : data));
					
				deferred.reject(data);
			});
			
			return deferred.promise;
		}catch(e){
			/* */
			log.error("widgetService: getDettaglioWidget ---> " + e);
		}
    }
    
}]);